<?php

class User extends Controller {
	var $auth;
	function User()	{
		parent::Controller();	
		$this->load->library('pear_auth', array('table'=>'users'));
		$this->auth = $this->pear_auth->auth_info();
	}
	function index() {
		$data["layout"] = "user/index.php";
		$data["auth"] = $this->auth;
		
		// Hottest doctors
		$sql  = "SELECT users.id AS uid, companies.name AS company, users.*, (SELECT COUNT(rating_comments.id) FROM rating_comments WHERE rating_comments.id_user=uid) AS cnt_comments ";
		$sql .= "FROM users ";
		$sql .= "JOIN companies ON users.id_company = companies.id ";
		$sql .= "WHERE users.acc_type = 'doctor' ";
		$sql .= "ORDER BY cnt_comments DESC ";
		$query = $this->db->query($sql);
		$data['hotest_doctors'] = $query->result_array();
		
		// Highest Rated doctors
		$sql  = "SELECT u.id AS uid, companies.name AS company, (SELECT SUM(value) FROM rating_data JOIN rating_comments ON rating_data.id_comment = rating_comments.id WHERE rating_comments.id_user = uid) AS rsum, u.*  ";
		$sql .= "FROM users u ";
		$sql .= "JOIN companies ON u.id_company = companies.id ";
		$sql .= "WHERE acc_type='doctor' ";
		$sql .= "ORDER BY rsum DESC ";
		$query = $this->db->query($sql);
		$data['highest_doctors'] = $query->result_array();
		
		
		$this->load->view('user', $data);
	}
	function profile() {
		$data["layout"] = "user/profile.php";
		$data["auth"] = $this->auth;
		$this->load->library("rapyd");
		$this->rapyd->load("dataedit");
		
		$edit = new DataEdit("details", "users");
		//$edit->db->where("id", $this->auth["additional"][0]['id']);
		$edit->back_uri = "users/profile";
//		$edit->back_save = true;
//		$edit->back_delete = false;
//		$edit->back_cancel_save = true;
//		$edit->back_cancel_delete = true;
		
//
//		$edit->username = new inputField("Username", "username");		
//		$edit->new_password = new inputField("New password<br>(leave blank if dont want change)", "new_password");		
//
//
		$edit->email = new inputField("E-mail", "email");
//		
		$edit->fname = new inputField("First name", "fname");
		$edit->mname = new inputField("Middle name", "mname");
		$edit->lname = new inputField("Last name", "lname");

		$edit->sex = new radiogroupField("Sex", "sex", array("m"=>"Male","f"=>"Female", "u"=>"Undefined"));
		$edit->sex->insertValue = "u";
		
		$edit->birth = new dateField("Birthday", "birth","us");
		$edit->fname->group = "Personal info";
		$edit->mname->group = "Personal info";
		$edit->lname->group = "Personal info";
		$edit->sex->group = "Personal info";
		$edit->birth->group = "Personal info";
				
		$edit->address = new inputField("Address", "address");
		$edit->address->group = "Location";
		$edit->city = new inputField("City", "city");
		$edit->city->group = "Location";
		$edit->state = new inputField("State", "state");
		$edit->state->group = "Location";
		$edit->zip = new inputField("Zip", "zip");
		$edit->zip->group = "Location";

		
		$edit->img  = new uploadField("Photo", "photo");
		$edit->img->upload_path = "/ratemydoctor/photos/";
		$edit->img->allowed_types = "jpg|gif|png";
		$edit->img->thumb = array (50, 100);
		$edit->img->group = "Picture upload";
		
		$edit->buttons("modify", "save", "undo");
		$edit->build();

		$data["crud"] = $edit->output;
		$data["rapyd_head"] = $this->rapyd->get_head();
//
//		if ($edit->on_success()){
//			if ($_POST['new_password'] != '')
//				$this->db->query("UPDATE users SET password=MD5('{$_POST['new_password']}') WHERE username = '{$_POST['username']}'");
//		}		
		
		$this->load->view('user', $data);
	}	
	// Ratings
	function doctor_details () {
		$this->load->model('Rating');
		$data["auth"] = $this->auth;
		if ($doc_info = $this->Rating->get_doctor_details($this->uri->segment(3))) {
			$data["doc_info"] = $doc_info;
			$data["layout"] = "user/doctor_details.php";
		}
		else{
			$data["title"] = "Doctor details error!";
			$data["message"] = "Sorry! No datails are available!";
			$data["layout"] = "user/mods/alert.php";
		}
		$this->load->view('user', $data);
	}
	function doctor_rate () {
		$this->load->model('Rating');
		
		$data["auth"] = $this->auth;
		
		if ($doc_info = $this->Rating->get_doctor_details($this->uri->segment(3))) {
			$data["doc_info"] = $doc_info;
			
			$query = $this->db->getwhere('rating_items');
			$data['rating_info'] =  $query->result_array();
			
			$data["layout"] = "user/doctor_rate.php";
			
			if ($_SERVER['REQUEST_METHOD'] == "POST"){
				if (!$data["auth"]['auth'])
					$user_id = 6;
				else 
					$user_id = $data["auth"]["additional"][0]['id'];
					
				// insert comment
				$sql  = "INSERT INTO rating_comments (id_user, id_rater, comment, changed) ";
				$sql .= "VALUES ('".$this->uri->segment(3)."','".$user_id."','".$this->input->post('comment', true)."',NOW()) ";
				$query = $this->db->query($sql);
				$ins_id = $this->db->insert_id();
				// insert rating data
				foreach ($_POST["ratingid"] AS $key => $item){
					$this->db->insert('rating_data', array('id_comment'=>$ins_id, 'id_item'=>$key, 'value'=>$item));
				}
			}
		}
		else{
			$data["title"] = "Doctor details error!";
			$data["message"] = "Sorry! No datails are available!";
			$data["layout"] = "user/mods/alert.php";
		}		
		$this->load->view('user', $data);
	}
	function tell_friend () {
		$data["layout"] = "user/tell_friend.php";
		$data["auth"] = $this->auth;
		$this->load->view('user', $data);	
	}
	// CMS
	function contact() {
		$this->load->library('cms');
		$data["layout"] = "user/cms.php";
		$data["auth"] = $this->auth;
		$data["cms"] = $this->cms->load_content('contact');
		$this->load->view('user', $data);
	}
	function about() {
		$this->load->library('cms');
		$data["layout"] = "user/cms.php";
		$data["auth"] = $this->auth;
		$data["cms"] = $this->cms->load_content('about');
		$this->load->view('user', $data);
	}
	function faq() {
		$this->load->library('cms');
		$data["layout"] = "user/cms.php";
		$data["auth"] = $this->auth;
		$data["cms"] = $this->cms->load_content('faq');
		$this->load->view('user', $data);
	}
	function rss() {
		$this->load->library('cms');
		$data["layout"] = "user/cms.php";
		$data["auth"] = $this->auth;
		$data["cms"] = $this->cms->load_content('rss');
		$this->load->view('user', $data);
	}
	function press() {
		$this->load->library('cms');
		$data["layout"] = "user/cms.php";
		$data["auth"] = $this->auth;
		$data["cms"] = $this->cms->load_content('press');
		$this->load->view('user', $data);
	}
	function terms() {
		$this->load->library('cms');
		$data["layout"] = "user/cms.php";
		$data["auth"] = $this->auth;
		$data["cms"] = $this->cms->load_content('terms');
		$this->load->view('user', $data);
	}
	function policy() {
		$this->load->library('cms');
		$data["layout"] = "user/cms.php";
		$data["auth"] = $this->auth;
		$data["cms"] = $this->cms->load_content('policy');
		$this->load->view('user', $data);
	}
	function copyright() {
		$this->load->library('cms');
		$data["layout"] = "user/cms.php";
		$data["auth"] = $this->auth;
		$data["cms"] = $this->cms->load_content('copyright');
		$this->load->view('user', $data);
	}
	// Loginform / forgot password
	function forgot() {
		$this->load->library('validation');
		$data["layout"] = "user/forgot.php";
		$data["auth"] = $this->auth;
		$fields['forg_email'] = 'E-mail';
		$this->validation->set_fields($fields);
		$rules['forg_email']    = "valid_email|callback_forgotten_email_check";
		$this->validation->set_rules($rules);
		if ($this->validation->run() == FALSE) {
			//echo "err";
		}
		else {
			$this->pear_auth->change_pass_n_send_email($this->input->post('forg_email', true));
			$data["title"] = "Forgotten password";
			$data["message"] = "The new password has been send to your e-mail!";
			$data["layout"] = "user/mods/alert.php";
		}

		$this->load->view('user', $data);
	}
	
	function logout(){
		$this->auth = $this->pear_auth->logout();
		redirect('user/index', 'refresh');
	}
	//callbacks
	function forgotten_email_check($str){
		$query = $this->db->getwhere('users', array('email' => $str));
		if ($query->num_rows() != 1) {
			$this->validation->set_message('forgotten_email_check', 'E-mail "'.$str.'" was not found in database. Try another one!');
			return FALSE;
		}
		else {
			return TRUE;
		}
	}
}
?>
